package com.example.month11.festivalsms.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.month11.festivalsms.ChooseMsgActivity;
import com.example.month11.festivalsms.bean.Festival;
import com.example.month11.festivalsms.bean.FestivalLab;
import com.example.recyclerview01.R;

/**
 * 节日分类。
 * ArrayAdapter 的使用。
 */
public class FestivalCategoryFragment extends Fragment {
    public static final String ID_FESTIVAL = "festival_id";

    private GridView mGridView;
    private ArrayAdapter<Festival> mAdapter;
    private LayoutInflater mInflater;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mInflater = inflater;
        View rootView = inflater.inflate(R.layout.fragment_festival_category, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mGridView = view.findViewById(R.id.id_gv_festival_category);
        // ArrayAdapter 的使用。
        mGridView.setAdapter(mAdapter = new ArrayAdapter<Festival>(getActivity(),
                -1, FestivalLab.getInstance().getFestivals()) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.item_festival, parent, false);
                }
                TextView tvName = convertView.findViewById(R.id.id_tv_festival_name);
                tvName.setText(getItem(position).getName());
                return convertView;
            }
        });

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                String item = mAdapter.getItem(position).getName();
//                Toast.makeText(getActivity(), "item:" + item, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), ChooseMsgActivity.class);
                intent.putExtra(ID_FESTIVAL, mAdapter.getItem(position).getId());
                startActivity(intent);
            }
        });
    }

}
